package entreprise;

import java.util.ArrayList;

import database.Database;
import mapping.DBCategory;

public class Category extends DBCategory {
	private Database database;

	public Category() {
		super();
		database = new Database("restoreservation");
	}

	public Category(String name_category) {
		super(name_category);
	}

	public Category(String id_category, String name_category) {
		super(id_category, name_category);
	}

	public Database getDatabase() {
		return database;
	}

	public void setDatabase(Database database) {
		this.database = database;
	}

	public void setDatabase(String db_name) {
		this.database = new Database(db_name);
	}

	public boolean addCategory() {
		String sql_str = "";
		try {
			sql_str = "INSERT INTO category(name_category) VALUES('"
					+ this.getName_category() + "')";
			database.set(sql_str);

		} catch (Exception ex) {
			ex.printStackTrace();
			return false;
		}

		return true;
	}

	public boolean updateCategory() {
		String sql_str = "";
		try {
			sql_str = "UPDATE category set name_category = '"
					+ this.getName_category() + "' WHERE id_category = "
					+ Integer.parseInt(this.getId_category());
			database.set(sql_str);
		} catch (Exception ex) {
			ex.printStackTrace();
			return false;
		}

		return true;
	}

	public boolean deleteCategory() {
		String sql_str = "";
		try {
			sql_str = "DELETE FROM category WHERE id_category = "
					+ Integer.parseInt(this.getId_category());
			database.set(sql_str);
		} catch (Exception ex) {
			ex.printStackTrace();
			return false;
		}

		return true;
	}

	public ArrayList<ArrayList<String>> listAllCategory() {
		String str_sql = "select * from category";
		ArrayList<ArrayList<String>> datas = new ArrayList<ArrayList<String>>();
		datas = database.get(str_sql);
		return datas;
	}

	public ArrayList<String> listCategoryWithName(String category) {
		String str_sql = "select * from category where name_category = '"
				+ category + "'";
		ArrayList<String> datas = new ArrayList<String>();
		int nb_col;
		if (database.get(str_sql).size() > 0) {
			nb_col = database.get(str_sql).get(0).size();
			for (int i = 0; i < nb_col; i++)
				datas.add(database.get(str_sql).get(0).get(i));
		}

		return datas;
	}

}
